package top.drinyeit.weilong.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lljjcoder.bean.CityBean;
import com.lljjcoder.bean.DistrictBean;
import com.lljjcoder.bean.ProvinceBean;
import com.lljjcoder.citywheel.CityConfig;
import com.lljjcoder.citywheel.CityPickerView;

import org.json.JSONException;
import org.json.JSONObject;

import top.drinyeit.weilong.R;
import top.drinyeit.weilong.base.BaseActivity;
import top.drinyeit.weilong.model.AddressDetailJsonBean;
import top.drinyeit.weilong.presenter.AddNewAddressPresenter;
import top.drinyeit.weilong.util.Validator;

/**
 * Created by bigHead on 2017/11/14.
 */

public class AddNewAddressActivity extends BaseActivity {

    CityPickerView cityPicker;
    private AddressDetailJsonBean addressDetailJsonBean;
    private TextView save_btn;
    private TextView place, detail, consignee_name, consignee_phone;
    private TextView number;

    private AddNewAddressPresenter addNewAddressPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_add_newaddress);
        save_btn = (TextView) findViewById(R.id.save_btn);
        place = findViewById(R.id.place);
        detail = findViewById(R.id.detail);
        consignee_name = findViewById(R.id.consignee_name);
        consignee_phone = findViewById(R.id.consignee_phone);
        number = findViewById(R.id.number);

        addNewAddressPresenter = new AddNewAddressPresenter(this);

        setTitle(true,"添加新地址");
        init();
    }


    @Override
    public void init() {
        save_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (consignee_name.getText().length() < 1) {
                    showToast("收货人不能为空");
                    return;
                }
                if (!Validator.isMobile(consignee_phone.getText().toString())) {
                    showToast("请填写正确的手机号");
                    return;
                }
                if (addressDetailJsonBean == null) {
                    showToast("请选择所在地");
                    return;
                }
                if (detail.getText().length() < 2) {
                    showToast("请填写详细地址");
                    return;
                } else {
                    addressDetailJsonBean.setDetail(detail.getText().toString());
                }

                addNewAddressPresenter.addNewAdd(consignee_name.getText().toString(), consignee_phone.getText().toString(), new Gson().toJson(addressDetailJsonBean));
            }
        });

        place.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    if (imm.isActive()) {
                        imm.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                    }
                }
                if (cityPicker == null) {
                    CityConfig cityConfig = new CityConfig.Builder(AddNewAddressActivity.this)
                            .textSize(18)//（滚轮文字的大小，int 类型，默认为18）
                            .title("地区选择")//（选择器标题，默认为“选择地区”）
                            .titleBackgroundColor("#ffffff")//（标题栏背景，默认为灰色，#C7C7C7）
                            .titleTextColor("#333333")//(标题文字颜色，默认为 #E9E9E9)
                            .confirTextColor("#666666")//（确认按钮字体颜色，默认为系统的colorPrimary颜色值）
                            .cancelTextColor("#666666")//（取消按钮字体颜色，默认为系统的colorPrimary颜色值）
                            .province("浙江")//（默认的显示省份，显示选择器后直接定位的item位置）
                            .city("温州")//（默认的显示市，显示选择器后直接定位的item位置）
                            .district("苍南县")//（默认的显示区，显示选择器后直接定位的item位置）
                            .textColor("#333333")//（滚轮文字的颜色 ，int 类型，默认为0xFF585858）
                            .provinceCyclic(true)//（省份的滚轮是否循环滚动）
                            .cityCyclic(false)//（市的滚轮是否循环滚动）
                            .districtCyclic(false)//（区的滚轮是否循环滚动）
                            .visibleItemsCount(7)//（滚轮显示的item个数，int 类型，默认为5个）
                            .itemPadding(10)//（滚轮item间距，默认为5dp）
                            .setCityInfoType(CityConfig.CityInfoType.BASE) //BASE:只包含省市区名称; DETAIL:包含详细的经纬度、code、省市区数据名称等数据
                            .setCityWheelType(CityConfig.WheelType.PRO_CITY_DIS)
                            .build();
                    cityPicker = new CityPickerView(cityConfig);
                    //监听方法，获取地址选择结果
                    cityPicker.setOnCityItemClickListener(new CityPickerView.OnCityItemClickListener() {
                        @Override
                        public void onSelected(ProvinceBean province, CityBean city, DistrictBean district) {
                            if (addressDetailJsonBean == null) {
                                addressDetailJsonBean = new AddressDetailJsonBean(province.getName(), city.getName() + "市", "",
                                        (district == null ? "" : district.getName()));
                            } else {
                                addressDetailJsonBean.setProvince(province.getName());
                                addressDetailJsonBean.setCity(city.getName() + "市");
                                addressDetailJsonBean.setRegion(district == null ? "" : district.getName());
                            }
                            place.setText(province.getName() + city.getName() + "市" + (district == null ? "" : district.getName()));
                            place.setTextColor(ContextCompat.getColor(AddNewAddressActivity.this, R.color.text_black));

                        }

                        @Override
                        public void onCancel() {
                        }
                    });
                }
                cityPicker.show();
            }
        });

        detail.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                number.setText(s.length() + "");
            }
        });
    }


}
